package com.cnebula.openapi.auth.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.cnebula.openapi.auth.exception.ErrorInfo;
import com.cnebula.openapi.auth.exception.ErrorInfoTable;
import com.cnebula.openapi.auth.exception.HeaderParamsMissing;
import com.cnebula.openapi.auth.exception.RequestParamsMissing;
import com.cnebula.openapi.auth.vo.ResponseResult;

import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

public class RestUtil {
	
	
	public static final String CONTENT_TYPE = "application/json; charset=utf-8";
	

	public static boolean checkRequired(Map<String, String> parameters, RoutingContext context) {
		
		boolean rst = true;
		
		for(Entry<String, String> e: parameters.entrySet()) {
			if (e.getValue() == null || e.getValue().isEmpty()) {
				rst = false;
				responseError(new RequestParamsMissing(e.getKey()), context);
				break;
			}
		}
		return rst;

	}
	

	public static boolean checkRequiredHeader(Map<String, String> parameters, RoutingContext context) {
		
		boolean rst = true;
		
		for(Entry<String, String> e: parameters.entrySet()) {
			if (e.getValue() == null || e.getValue().isEmpty()) {
				rst = false;
				responseError(new HeaderParamsMissing(e.getKey()), context);
				break;
			}
		}
		return rst;
	}
	
	public static void responseResult(ResponseResult result, RoutingContext context) {
		HttpServerResponse response = context.response();
		response.setStatusCode(200).putHeader("content-type", CONTENT_TYPE).end(result.toString());
	}
	
	public static void responseResult(String message, RoutingContext context) {
		HttpServerResponse response = context.response();
		response.setStatusCode(200).putHeader("content-type", CONTENT_TYPE).end(message);
	}

	public static void redirect(String url, RoutingContext context) {

		HttpServerResponse response = context.response();

		response.putHeader("location", url).setStatusCode(302).end();
	}
	
//	public static void reponseSuccess(RoutingContext context) {
//		HttpServerResponse response = context.response();
//		ResponseResult rst = new ResponseResult();
//		response.setStatusCode(200).putHeader("content-type", CONTENT_TYPE).end(rst.toString());
//	}
//	
//	public static void reponseSuccess(String message, RoutingContext context) {
//		HttpServerResponse response = context.response();
//		response.setStatusCode(200).putHeader("content-type", CONTENT_TYPE).end(message);
//	}

//	public static void reponseError(String code, String message, RoutingContext context) {
//		HttpServerResponse response = context.response();
//		ResponseResult rst = new ResponseResult(code, message);
//		response.setStatusCode(200).putHeader("content-type", CONTENT_TYPE).end(rst.toString());
//	}

	public static void responseError(Throwable e, RoutingContext context) {
		HttpServerResponse response = context.response();
		String message = e.getMessage();
		
		int statusCode = 500;
		
		if(ErrorInfoTable.isErrorInfos400(e.getClass().getName())) {
			statusCode = 400;
		}
		
		if(!ErrorInfoTable.isErrorInfos(e.getClass().getName())){
			
			ResponseResult rst = new ResponseResult(ErrorInfo.SYS_UNKNOW, ErrorInfo.SYS_UNKNOW_INFO);
			message = rst.toString();
		}
		response.setStatusCode(statusCode).putHeader("content-type", CONTENT_TYPE).end(message);
	}
	
	
	public static Map<String,String> collectParams(RoutingContext context){
		Map<String,String> rst = new HashMap<String,String>();
		HttpServerRequest request = context.request();
		request.params().forEach(e -> {
			rst.put(e.getKey(), e.getValue());
		});
		
		return rst;
	}

}
